/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.GUI;

import edu.nii.qlks.ControlLayer.PhuThuControl;
import edu.nii.qlks.DataTransferObject.PhuThuInfo;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author 10520_000
 */
public class jfrm_ThayDoiTyLePhuThu extends javax.swing.JInternalFrame {

    /**
     * Creates new form jfrm_ThayDoiTyLePhuThu
     */
    PhuThuControl phuthuCT;
    PhuThuInfo phuthuInfo;

    public jfrm_ThayDoiTyLePhuThu() {
        initComponents();
        phuthuCT = new PhuThuControl();
        phuthuInfo = new PhuThuInfo();
        
        Load_ThuTuKhachTinhPhuThu();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jXLabel2 = new org.jdesktop.swingx.JXLabel();
        jXLabel3 = new org.jdesktop.swingx.JXLabel();
        jComboBox_ThuTuKhachTinhPhuThu = new javax.swing.JComboBox();
        jTextField_TyLePhuThu = new javax.swing.JTextField();
        jButton_CapNhat = new javax.swing.JButton();
        jButton_Thoat = new javax.swing.JButton();
        jXLabel1 = new org.jdesktop.swingx.JXLabel();

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jXPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jXPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin phụ thu", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jXLabel2.setText("Thứ tự khách tính phụ thu");
        jXLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXLabel3.setText("Tỷ lệ phụ thu");
        jXLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jComboBox_ThuTuKhachTinhPhuThu.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jComboBox_ThuTuKhachTinhPhuThu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_ThuTuKhachTinhPhuThuActionPerformed(evt);
            }
        });

        jTextField_TyLePhuThu.setToolTipText("Nhập tỷ lệ phụ thu");
        jTextField_TyLePhuThu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_TyLePhuThuActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jXPanel1Layout = new javax.swing.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel1Layout.createSequentialGroup()
                .addContainerGap(64, Short.MAX_VALUE)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(56, 56, 56)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField_TyLePhuThu)
                    .addComponent(jComboBox_ThuTuKhachTinhPhuThu, 0, 158, Short.MAX_VALUE))
                .addGap(70, 70, 70))
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox_ThuTuKhachTinhPhuThu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField_TyLePhuThu, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42))
        );

        jButton_CapNhat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_CapNhat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/refresh.png"))); // NOI18N
        jButton_CapNhat.setText("Cập nhật");
        jButton_CapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CapNhatActionPerformed(evt);
            }
        });

        jButton_Thoat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_Thoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
        jButton_Thoat.setText("Đóng");
        jButton_Thoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ThoatActionPerformed(evt);
            }
        });

        jXLabel1.setText("THAY ĐỔI TỶ LỆ PHỤ THU");
        jXLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(80, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton_CapNhat)
                        .addGap(18, 18, 18)
                        .addComponent(jButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(96, 96, 96))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(73, 73, 73))))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(207, 207, 207)
                .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_CapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_Thoat))
                .addGap(46, 46, 46))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    public void Load_ThuTuKhachTinhPhuThu(){
        try {
            Vector vt = phuthuCT.LayDS();
            if (vt.size() > 0){
                for (int i = 0; i < vt.size(); i++){
                    Vector vt_row = (Vector)vt.get(i);
                    String thutukhachtinhphuthu = vt_row.get(1).toString().trim();
                    jComboBox_ThuTuKhachTinhPhuThu.addItem(thutukhachtinhphuthu);
                }
            }
            else{//////////
            }
        } catch (Exception ex) {
            Logger.getLogger(jfrm_CapNhatDonGiaLoaiPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    private void jButton_CapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CapNhatActionPerformed
        // TODO add your handling code here:
        if(jTextField_TyLePhuThu.getText().equals("")){
            JOptionPane.showMessageDialog(this, "Bạn chưa nhập tỷ lệ phụ thu!", "Lỗi", JOptionPane.ERROR_MESSAGE);
        }
        else{
           try{
               phuthuInfo.setThuTuKhachTinhPhuThu(Integer.parseInt(jComboBox_ThuTuKhachTinhPhuThu.getSelectedItem().toString().trim())); 
               phuthuInfo.setTylePhuThu(Float.parseFloat(jTextField_TyLePhuThu.getText().trim()));
                try {
                    String str = jComboBox_ThuTuKhachTinhPhuThu.getSelectedItem().toString().trim();
                    Vector vt = phuthuCT.LayDS("ThuTuKhachTinhPhuThu = N'" + str + "'");
                    Vector vt_row = (Vector) vt.get(0);
                    phuthuInfo.setMaPhuThu(vt_row.get(0).toString().trim());

                   phuthuCT.Update(phuthuInfo);

                    JOptionPane.showMessageDialog(this, "Cập nhật thành công!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                } catch (Exception ex) {
                    Logger.getLogger(jfrm_ThayDoiTyLePhuThu.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(this, "Cập nhật thất bại!", "Lỗi", JOptionPane.ERROR_MESSAGE);
                    jTextField_TyLePhuThu.setText(null);
                    jTextField_TyLePhuThu.requestFocus();
                    return;
                }
           }
           catch (Exception e){
               JOptionPane.showMessageDialog(this, "Hệ số phụ thu chỉ được phép nhập số!", "Lỗi", JOptionPane.ERROR_MESSAGE);
               jTextField_TyLePhuThu.requestFocus();
               jTextField_TyLePhuThu.setText("");
               return;
           }
        }
    }//GEN-LAST:event_jButton_CapNhatActionPerformed

    private void jButton_ThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton_ThoatActionPerformed

    private void jComboBox_ThuTuKhachTinhPhuThuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_ThuTuKhachTinhPhuThuActionPerformed
       if (jComboBox_ThuTuKhachTinhPhuThu.getItemCount() > 0){
            try {
                String str = jComboBox_ThuTuKhachTinhPhuThu.getSelectedItem().toString().trim();
                Vector vt = phuthuCT.LayDS("ThuTuKhachTinhPhuThu = N'" + str + "'");
                Vector vt_row = (Vector)vt.get(0);
                jTextField_TyLePhuThu.setText(vt_row.get(2).toString().trim());
            } catch (Exception ex) {
                //
            }
        }
    }//GEN-LAST:event_jComboBox_ThuTuKhachTinhPhuThuActionPerformed

    private void jTextField_TyLePhuThuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField_TyLePhuThuActionPerformed
        jButton_CapNhatActionPerformed(evt);
    }//GEN-LAST:event_jTextField_TyLePhuThuActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_CapNhat;
    private javax.swing.JButton jButton_Thoat;
    private javax.swing.JComboBox jComboBox_ThuTuKhachTinhPhuThu;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField_TyLePhuThu;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel2;
    private org.jdesktop.swingx.JXLabel jXLabel3;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    // End of variables declaration//GEN-END:variables
}
